package co2.bean;

import java.io.IOException;
import java.io.Serializable;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import co2.engine.DAOInterface;
import co2.pojos.MstUser;


@ManagedBean
@SessionScoped
public class MasterBean implements Serializable{
	private static final long serialVersionUID = 1L;
	
	private DAOInterface dao;
	private String fullName;
	private SignInBean signInBean;
	private MstUser user;
	
	public MasterBean(){
		try{
			Map map = (Map) FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
			signInBean = (SignInBean) map.get("signInBean");
			user = signInBean.getTheUser();
			dao = signInBean.getDao();
			fullName = user.getUsername();
			
		}catch(Exception e){
			e.printStackTrace();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"ERROR!", "Error cause: "+e.getCause()+" Message: "+e.getMessage()));
			 
		}
	}
	
	public void logout(){
		

		try {
			String extUrl=FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();

			FacesContext.getCurrentInstance().getExternalContext().redirect(extUrl+"/jsf/target/front_ui.jsf");
			

			FacesContext.getCurrentInstance().getExternalContext().getSession(false);
			HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
			session.invalidate();

			
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public DAOInterface getDao() {
		return dao;
	}

	public void setDao(DAOInterface dao) {
		this.dao = dao;
	}

	public String getFullName() {
		return fullName;
	}

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	public SignInBean getSignInBean() {
		return signInBean;
	}

	public void setSignInBean(SignInBean signInBean) {
		this.signInBean = signInBean;
	}
}
